<?php
if (!defined("BASEPATH")) exit("No direct script access allowed");

class Usr2ent extends MY_Model {

	function __construct() {
		parent::__construct();
		$this->tableName = "usr2ent";
		$this->prefix = "u2e";
	}

	function getUsr2ent($where = NULL) {

		$qs =	"
					select
						u2e_id, u2e_usr_id, u2e_cou_id, u2e_city_id, u2e_ins_id, u2e_page_cat, coalesce(u2e_cou_id, u2e_city_id, u2e_ins_id, u2e_page_cat) as ID
					from
						".$this->db->dbprefix.$this->tableName."
					where
						u2e_del = 0".((!is_null($where))?(" and ".$where):(""));
		$result = $this->db->query($qs);
		$getUsr2ent = array();
		$this->load->model("institute");
		if($result->num_rows()) {
			foreach ($result->result() as $row) {
				$type = "";
				if($row->u2e_cou_id) $type = "COU";
				elseif($row->u2e_city_id) $type = "CITY";
				elseif($row->u2e_ins_id and !$row->u2e_page_cat) $type = "INS";
				elseif($row->u2e_page_cat) $type = "PAGE_CAT_".$row->u2e_page_cat;
				if($row->u2e_ins_id and $row->u2e_page_cat) {
					$getUsr2ent[$type]["u2eIds"][$row->u2e_ins_id] = $row->u2e_id;
				} else {
					$getUsr2ent[$type]["u2eIds"][] = $row->u2e_id;
				}
				$getUsr2ent[$type]["children"][] = $row->ID;
				
				// ins esetben minden gyerek legyüjtése
				/*
				$getInstitute = $this->institute->getInstitute("ins_ins_id = '".$row->ID."'");
				while(count($getInstitute)) {
					$insIds = "";
					foreach($getInstitute as $ins) {
						$getUsr2ent[$type]["children"][] = $ins["insId"];
						$insIds .= (($insIds)?(","):("")) . $ins["insId"];
					}
					$getInstitute = $this->institute->getInstitute("ins_ins_id in (".$insIds.")");
				}
				*/
			}
		}

		return $getUsr2ent;
	}


	/*function getEntList2User($where = NULL) {

		$qs =	"
					select
						
					from
						".$this->db->dbprefix.$this->tableName."
						left join ".$this->db->dbprefix."country on cou_id = u2e_cou_id and cou_del = 0
						left join ".$this->db->dbprefix."city as a on a.city_id = u2e_city_id and a.city_del = 0
						left join ".$this->db->dbprefix."institute on ins_id = u2e_ins_id and ins_del = 0
						left join ".$this->db->dbprefix."city as b on b.city_id = ins_city_id and b.city_del = 0
					where
						u2e_del = 0".((!is_null($where))?(" and ".$where):(""));

	}*/
	
	function getUserList2Ent($where = NULL) {
		$qs =	"
					select
						coalesce(e2c_int, e2c_text, usr_email) as name, usr_email
					from
						".$this->db->dbprefix.$this->tableName."
						join ".$this->db->dbprefix."user on usr_id = u2e_usr_id and usr_del = 0
						join ".$this->db->dbprefix."usr2grp on usr_id = u2g_usr_id and u2g_del = 0 and u2g_grp_id = 2
						left join ".$this->db->dbprefix."ent2cat on e2c_ent_id = usr_id and e2c_entity = 'USER' and e2c_cat_id = '".$this->db->escape_str($this->config->item("categoryUSRFullname"))."' and e2c_del = 0
						left join ".$this->db->dbprefix."country on cou_id = u2e_cou_id and cou_del = 0
						left join ".$this->db->dbprefix."city on city_id = u2e_city_id and city_del = 0
						left join ".$this->db->dbprefix."institute on ins_id = u2e_ins_id and ins_del = 0
					where
						u2e_del = 0".((!is_null($where))?(" and ".$where):(""))."
					group by
						usr_id
					order by
						coalesce(e2c_int, e2c_text, usr_email), usr_email
					";
		$result = $this->db->query($qs);
		$getUserList2Ent = array();
		if($result->num_rows()) {
			foreach ($result->result() as $row) {
				$getUserList2Ent[$row->usr_email] = $row->name.(($row->name != $row->usr_email)?(" (".$row->usr_email.")"):(""));
			}
		}
		return $getUserList2Ent;
	}

}